// ==UserScript==
// @name         Pixiv api caller for mobile
// @namespace    http://tampermonkey.net/
// @version      0.0.1
// @description  pixiv 体验增强
// @author       HqLin
// @match        https://www.pixiv.net/*
// @grant        GM_xmlhttpRequest
// @grant        GM_setClipboard
// @grant        GM_addStyle
// ==/UserScript==
'use strict';

(async () => {
    GM_addStyle(`
    .premium-lead-new-t-info-home-top,
    .premium-lead-t-popular-d-body,
    .premium-lead-t-footer,
    .ad-topmost-header,
    .ad-container,
    #ad-header,
    .ad-frame-container {
        display: none!important;
    }
    `)

    let ID = getID()
    let DATA = await reFetch()

    // document.body.addEventListener("keydown", async ev => {
    //     if (ev.altKey && DATA) {
    //         ev.preventDefault()
    //         const currentID = getID()

    //         // 缓存更新
    //         if (currentID != ID) {
    //             DATA = await reFetch()
    //             ID = currentID
    //         }
    //         const page = DATA["novel"][ID]

    //         switch (ev.key) {
    //             case "y": {
    //                 GM_setClipboard((page["content"] as string)
    //                     .replaceAll(/\n(?!\n)/g, "\n\n"))
    //                 break
    //             }
    //             case "t": {
    //                 const taginfo = page["tags"]["tags"].map((info: {tag: any;}) =>
    //                     info.tag.includes(" ") ? `[[${info.tag}]]` : info.tag
    //                 )
    //                 GM_setClipboard(taginfo.join(" "))
    //                 break
    //             }
    //             case "l": {
    //                 GM_setClipboard(document.URL)
    //                 break
    //             }
    //             case "n": {
    //                 let series = page["seriesNavData"]
    //                 GM_setClipboard((series) ? `${series["title"]}——${page["title"]}` : page["title"])
    //                 break
    //             }
    //             default: return
    //         }
    //     }
    // })

    async function reFetch(): Promise<any> {
        let url = new URL("https://www.pixiv.net/novel/show.php")
        let Id = getID()
        url.searchParams.append("id", Id)
        const doc: Document = await rq({url: url.toString()})
        return JSON.parse(doc.querySelector("meta#meta-preload-data")?.getAttribute("content") ?? "")
    }

    function getID(): string {
        return new URL(document.URL).searchParams.get("id") ?? ""
    }

    async function rq(details: Tampermonkey.Request, timeout = 5000): Promise<Document> {
        return new Promise((res, rej) => {
            details.onerror = rej;
            details.ontimeout = rej;
            details.timeout = timeout;

            details.responseType = "document";
            details.onload = resp => {
                if (resp.status != 200) rej("Not 200!");
                res(resp.response);
            };
            GM_xmlhttpRequest(details);
        });
    }

})()
